package rbac

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"store/logic"
	"store/models/rbac"
)

type AdminRoleLogic struct {
	logic.RestLogic
	Ctx *gin.Context
}

type AdminRoleInput struct {
	logic.XmlInput
	RoleName string `json:"roleName" xml:"roleName" form:"roleName" field:"role_name" binding:"max=32" msg:"角色名称最大长度为32"`
	IsOn     uint8  `json:"isOn" xml:"isOn" form:"isOn" field:"is_on" binding:"min=0" msg:"是否启用必须是非负数"`
	Id       uint64 `json:"id" xml:"id" form:"id" field:"id" binding:"min=0" msg:"角色id必须是非负数"`
}

/**克隆logic,用于存放*gin.Context
 * @email jhq0113@163.com
 * generated by gii
 */
func (this *AdminRoleLogic) Clone(ctx *gin.Context) logic.RestLogic {
	newLogic := *this
	newLogic.Ctx = ctx
	return &newLogic
}

/**解析http输入
 * @email jhq0113@163.com
 * generated by gii
 */
func (this *AdminRoleLogic) LoadInput() (input interface{}, err error) {
	in := AdminRoleInput{
		IsOn: 1,
	}
	err = this.Ctx.ShouldBind(&in)
	return in, err
}

/**解析http输入转化为update输入
 * @email jhq0113@163.com
 * generated by gii
 */
func (this *AdminRoleLogic) LoadInput4Update(where map[string]interface{}) (input interface{}, version uint64, err error) {
	model, ok := this.View(where)
	if !ok {
		err = fmt.Errorf("not found")
		return
	}

	val := model.(rbac.AdminRole)
	value := AdminRoleInfo2Input(&val)
	err = this.Ctx.ShouldBind(&value)
	input = value
	version = val.Version
	return
}

/**创建输入
 * @email jhq0113@163.com
 * generated by gii
 */
func (this *AdminRoleLogic) NewCreateInput() interface{} {
	return AdminRoleInput{
		IsOn: 1,
	}
}

/**数据库Model转Input
 * @email jhq0113@163.com
 * generated by gii
 */
func AdminRoleInfo2Input(info *rbac.AdminRole) AdminRoleInput {
	return AdminRoleInput{
		IsOn:     info.IsOn,
		Id:       info.Id,
		RoleName: info.RoleName,
	}
}

/**插入
 * @email jhq0113@163.com
 * generated by gii
 */
func (this *AdminRoleLogic) Add(input interface{}) (id uint64) {
	in := input.(*interface{})
	info := logic.Input2Map((*in).(AdminRoleInput))
	id = rbac.AdminRoleCreate(this.Ctx, info)
	if id > 0 {
		ResetAllEnableRoles(this.Ctx)
	}
	return id
}

/**删除
 * @email jhq0113@163.com
 * generated by gii
 */
func (this *AdminRoleLogic) Delete(id uint64) (rows int64) {
	rows = rbac.AdminRoleDelete(this.Ctx, id)
	if rows > 0 {
		ResetAllEnableRoles(this.Ctx)
	}
	return rows
}

/**修改
 * @email jhq0113@163.com
 * generated by gii
 */
func (this *AdminRoleLogic) Update(version uint64, input interface{}) (rows int64) {
	infoInput := input.(AdminRoleInput)
	info := logic.Input2Map(infoInput)
	rows = rbac.AdminRoleUpdate(this.Ctx, infoInput.Id, version, info)
	if rows > 0 {
		ResetAllEnableRoles(this.Ctx)
	}
	return rows
}

/**启用禁用
 * @email jhq0113@163.com
 * generated by gii
 */
func (this *AdminRoleLogic) UpOrDown(id uint64, value uint8) (rows int64) {
	rows = rbac.AdminRoleUpOrDown(this.Ctx, id, value)
	if rows > 0 {
		ResetAllEnableRoles(this.Ctx)
	}

	return rows
}

/**详情
 * @email jhq0113@163.com
 * generated by gii
 */
func (this *AdminRoleLogic) View(where map[string]interface{}) (info interface{}, exist bool) {
	model := rbac.AdminRoleInfo(this.Ctx, where)
	return model, model.Id > 0
}

/**列表分页检索
 * @email jhq0113@163.com
 * generated by gii
 */
func (this *AdminRoleLogic) Index(page int64, pageSize int64, where map[string]interface{}, orders []string) (pageData map[string]interface{}) {
	return rbac.AdminRoleIndex(this.Ctx, page, pageSize, where, orders)
}
